/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/5/25
 * Time: 19:40
 */

public class Solution {
    private static int i = 0;
    public static TreeNode createTree(int[] array){
        int cur = array[i];
        TreeNode root = null;
        if(cur == -1){
            i++;
        }else {
            root = new TreeNode(cur);
            i++;
            root.left = createTree(array);
            root.right = createTree(array);
        }
        return root;
    }
    public static boolean isValidBST(TreeNode root) {
        return judgeTree(root,null,null);
    }
    public static boolean judgeTree(TreeNode root,Integer lower,Integer upper) {
        if(root == null){
            return true;
        }
        int val = root.val;
        if(lower != null && val <= lower){
            return false;
        }
        if(upper != null && val >= upper){
            return false;
        }
        //递归左树,替换上界
        if(!judgeTree(root.left,lower,val)) {
            return false;
        }
        //递归右树,替换下界
        if(!judgeTree(root.right,val,upper)) {
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        TreeNode root = createTree(new int[]{2,1,-1,-1,3,-1,-1});
        System.out.println(isValidBST(root));
    }
}